package dao;

import java.util.*;
import java.util.stream.Collectors;
import model.*;

public class DataManager {
    private static DataManager instance;
    private List<Student> students;
    private List<ClassRoom> classes;
    private List<Group> groups;
    private List<DataChangeListener> listeners;
    
    private DataManager() {
        students = new ArrayList<>();
        classes = new ArrayList<>();
        groups = new ArrayList<>();
        listeners = new ArrayList<>();
    }
    
    public static DataManager getInstance() {
        if (instance == null) {
            instance = new DataManager();
        }
        return instance;
    }
    
    // 添加数据变化监听器
    public void addDataChangeListener(DataChangeListener listener) {
        listeners.add(listener);
    }
    
    // 通知所有监听器数据已变化
    private void notifyDataChanged() {
        for (DataChangeListener listener : listeners) {
            listener.onDataChanged();
        }
    }
    
    // 班级管理方法
    public void addClass(ClassRoom classRoom) {
        classes.add(classRoom);
        notifyDataChanged();
    }
    
    public void removeClass(int classId) {
        classes.removeIf(c -> c.getId() == classId);
        notifyDataChanged();
    }
    
    public List<ClassRoom> getAllClasses() {
        return new ArrayList<>(classes);
    }
    
    // 小组管理方法
    public void addGroup(Group group) {
        groups.add(group);
        notifyDataChanged();
    }
    
    public void removeGroup(int groupId) {
        groups.removeIf(g -> g.getId() == groupId);
        notifyDataChanged();
    }
    
    public List<Group> getGroupsByClass(int classId) {
        return groups.stream()
            .filter(g -> g.getClassId() == classId)
            .collect(Collectors.toList());
    }
    
    // 学生管理方法
    public void addStudent(Student student) {
        students.add(student);
    }
    
    public void removeStudent(int studentId) {
        students.removeIf(s -> s.getId() == studentId);
    }
    
    public List<Student> getStudentsByClass(int classId) {
        return students.stream()
            .filter(s -> s.getClassId() == classId)
            .collect(Collectors.toList());
    }
    
    public List<Student> getStudentsByGroup(int groupId) {
        return students.stream()
            .filter(s -> s.getGroupId() == groupId)
            .collect(Collectors.toList());
    }
    
    // 更新班级信息
    public void updateClass(ClassRoom updatedClass) {
        for (int i = 0; i < classes.size(); i++) {
            if (classes.get(i).getId() == updatedClass.getId()) {
                classes.set(i, updatedClass);
                break;
            }
        }
        notifyDataChanged();
    }
    
    public void updateGroup(Group updatedGroup) {
        for (int i = 0; i < groups.size(); i++) {
            if (groups.get(i).getId() == updatedGroup.getId()) {
                groups.set(i, updatedGroup);
                break;
            }
        }
        notifyDataChanged();
    }
    
    public List<Group> getAllGroups() {
        return new ArrayList<>(groups);
    }
    
    public void updateStudent(Student updatedStudent) {
        for (int i = 0; i < students.size(); i++) {
            if (students.get(i).getId() == updatedStudent.getId()) {
                students.set(i, updatedStudent);
                break;
            }
        }
        notifyDataChanged();
    }
    
    public Student getStudent(int studentId) {
        return students.stream()
            .filter(s -> s.getId() == studentId)
            .findFirst()
            .orElse(null);
    }
    
    public List<Student> getAllStudents() {
        return new ArrayList<>(students);
    }
    
    public Group getGroup(int groupId) {
        return groups.stream()
            .filter(g -> g.getId() == groupId)
            .findFirst()
            .orElse(null);
    }
} 